package memory;

/**
 * Class for representing a single square of a memory board
 * @author Scott Robinson
 */
public class Square {
    private final int row;
    private final int column;
    private int pairValue;
    private boolean paired;

    /**
     * Constructor that takes the row and column location as well as the internal
     * integer that will be used to match squares.
     * @param row the row location of this square
     * @param column the column location of this square
     * @param pairValue the integer that will be used internally to match squares
     */
    public Square(int row, int column, int pairValue) {
        this.row = row;
        this.column = column;
        this.pairValue = pairValue;
        this.paired = false;
    }

        /**
     * Alternate constructor that takes no pair value and sends a pair value of "0"
     * back to the other constructor.
     * @param row
     * @param column
     */
    public Square(int row, int column) {
        this (row, column, 0);
    }

    /**
     * Boolean mathcing method
     * @param s the square to which the existing square needs to be matched too
     * @return a boolean representation of whether or not the squares match
     */
    public boolean isMatch(Square s) {
        return s.pairValue == this.pairValue;
    }

    /**
     * This method will flag the square as being matched or paired up with another square
     * on the board.
     */
    public void match() {
        this.paired = true;
    }

    /**
     * This sets the pairValue or the internal integer representation of the square
     * @param v the integer value to set the pair value too
     */
    protected void setPairValue(int v) {
        this.pairValue = v;
    }

    /**
     * This returns a boolean value representing whether or not this Square has been paired or not.
     * @return returns a boolean value representing whether or not this Square has been paired or not
     */
    public boolean isPaired() {
        return new Boolean(paired);
    }

    /**
     * A method that returns the interger pairing value for this square.
     * @return method that returns the interger pairing value for this square.
     */
    public int getPairValue() {
        return pairValue;
    }

    /**
     * Returns the column location of the square.
     * @return The column location of the square.
     */
    public int getColumn() {
        return column;
    }

    /**
     * Returns the row location of the square.
     * @return The row location of the square.
     */
    public int getRow() {
        return row;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Square other = (Square) obj;
        if (this.pairValue != other.pairValue) {
            return false;
        }
        if (this.row != other.row) {
            return false;
        }
        if (this.column != other.column) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 31 * hash + this.pairValue;
        hash = 31 * hash + this.row;
        hash = 31 * hash + this.column;
        return hash;
    }

    @Override public String toString () {
        return Integer.toString(pairValue);
    }

}
